﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Manageweb_Coffee_manageCoffee : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            BindCoffeeList();

        }
        InitEvent();
    }

    private void InitEvent()
    { 
    
      switch(this.hidOperationName.Value.ToUpper())
      {
          case "ADD":
              this.Btn_Submit.Click += new EventHandler(AddCoffee);
              break;
          case "MOD":
              this.Btn_Submit.Click += new EventHandler(ModCoffee);
              break;
          default:
              this.Btn_Submit.Click += new EventHandler(AddCoffee);
              break;
      }
    }

    private void BindCoffeeList()
    {
        SQLExpressDAL.CoffeeTableAdapters.ProductCoffeeTableAdapter adp = new SQLExpressDAL.CoffeeTableAdapters.ProductCoffeeTableAdapter();
        SQLExpressDAL.Coffee.ProductCoffeeDataTable tab = adp.GetCoffeeList();

        if (tab.Rows.Count==0)
        {
          
            this.TABLE_Add_Mod.Visible = true;
        }

        this.GV_CoffeList.DataSource = tab;
        this.GV_CoffeList.DataBind();
    }

    private void AddCoffee(object sender, EventArgs e)
    {
        string msg = "添加成功";

        try
        {
            string coffeeName = this.txtCoffeeName.Text.Trim();
            decimal coffeePrice = Convert.ToDecimal(this.txtCoffeePrice.Text.Trim());
            int coffeeCount = Convert.ToInt16(this.txtCoffeeCount.Text.Trim());
            int flag = 0;

            SQLExpressDAL.CoffeeTableAdapters.ProductCoffeeTableAdapter adp = new SQLExpressDAL.CoffeeTableAdapters.ProductCoffeeTableAdapter();
            flag = adp.InsertCoffee(coffeeName, coffeePrice, coffeeCount, 0);

            if (flag < 1)
            {
                msg = "添加失败";
            }
        }
        catch (Exception ex)
        {
            msg = "添加失败";
        }
        finally
        {
            Page.ClientScript.RegisterStartupScript(this.GetType(), "msg", "alert('" + msg + "');", true);
            BindCoffeeList();
            ClearTxt();
            this.TABLE_Add_Mod.Visible = false;
        }



    }

    private void ModCoffee(object sender, EventArgs e)
    {
        string msg = "修改成功";

        try
        {
            string coffeeName = this.txtCoffeeName.Text.Trim();
            decimal coffeePrice = Convert.ToDecimal(this.txtCoffeePrice.Text.Trim());
            int coffeeCount = Convert.ToInt16(this.txtCoffeeCount.Text.Trim());
            int coffeeRecordID = Convert.ToInt16(this.hidCoffeeID.Value);
            int flag = 0;

            SQLExpressDAL.CoffeeTableAdapters.ProductCoffeeTableAdapter adp = new SQLExpressDAL.CoffeeTableAdapters.ProductCoffeeTableAdapter();
            flag = adp.UpdateCoffeeByID(coffeeName, coffeePrice, coffeeCount, coffeeRecordID);

            if (flag < 1)
            {
                msg = "修改失败";
            }
        }
        catch (Exception ex)
        {
            msg = "修改失败";
        }
        finally
        {
            Page.ClientScript.RegisterStartupScript(this.GetType(), "msg", "alert('" + msg + "');", true);
            BindCoffeeList();
            ClearTxt();
            this.TABLE_Add_Mod.Visible = false;
        }
    }

    protected void LBtn_Add_Click(object sender, EventArgs e)
    {
        
        this.hidOperationName.Value = "ADD";
        this.TABLE_Add_Mod.Visible = true;
    }


    protected void LBtn_Mod_Command(object sender, CommandEventArgs e)
    {
        String[] coffeeInfo = e.CommandArgument.ToString().Split(new char[] { '|' });

        this.txtCoffeeName.Text = coffeeInfo[0];
        this.txtCoffeeCount.Text = coffeeInfo[1];
        this.txtCoffeePrice.Text = coffeeInfo[2];
        this.hidCoffeeID.Value = coffeeInfo[3];
        this.hidOperationName.Value = "MOD";
      
        this.TABLE_Add_Mod.Visible = true;
    }


    protected void LBtn_Del_Command(object sender, CommandEventArgs e)
    {
        string msg = "删除成功";
        try
        {
            int recordID = Convert.ToInt16(e.CommandArgument);
            int flag = 0;
            SQLExpressDAL.CoffeeTableAdapters.ProductCoffeeTableAdapter adp = new SQLExpressDAL.CoffeeTableAdapters.ProductCoffeeTableAdapter();
            flag = adp.DeleteCoffeeByID(recordID);

            if (flag < 1)
            {
                msg = "删除失败";
            }

        }
        catch (Exception ex)
        {
            msg = "删除失败";
        }
        finally
        {
            Page.ClientScript.RegisterStartupScript(this.GetType(), "msg", "alert('" + msg + "');", true);
            BindCoffeeList();
            ClearTxt();
        }


    }


    protected void ClearTxt()
    {
        this.txtCoffeeName.Text = string.Empty;
        this.txtCoffeeCount.Text = string.Empty;
        this.txtCoffeePrice.Text = string.Empty;
        this.hidCoffeeID.Value = string.Empty;
    }
    protected void GV_UserList_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        this.GV_CoffeList.PageIndex = e.NewPageIndex;
        BindCoffeeList();
    }
}

